<template>
  <div class="app-container">
    <div v-show="!detailInfo.visible && !modifyDeliverInfo.visible">
      <!-- <div class="width100"> -->
      <!-- 条件栏 -->
      <div class="filter-wrapper">
        <new-page-filter
          v-model:query="filterInfo.query"
          :clearType="clearType"
          :filter-list="filterInfo.searchList"
          :filter-btn="filterInfo.searchBtn"
        >
        </new-page-filter>
      </div>

      <!-- 表格 -->

      <table-container
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :api="tableInfo.api"
        :tab-index="true"
        :checkBox="false"
        :query="filterInfo.query"
        :field-list="tableInfo.fieldList"
        :handle="tableInfo.handle"
        @sortChange="sortChange"
        @handleEvent="handleEvent"
        @pageInfo="pageInfo"
      >
        <template v-slot:col-orderNo="scope">
          <span class="slip-edit" @click="showDetail(scope.row)">{{
            scope.row.orderNo || '-'
          }}</span>
        </template>
      </table-container>
    </div>
    <InvoiceDetail
      v-if="detailInfo.visible"
      :id="detailInfo.id"
      v-model:visible="detailInfo.visible"
      :type="'pendDealAssetDeliverOrder'"
      @returnBack="getList"
    />
    <ModifyOrder
      v-if="modifyDeliverInfo.visible"
      v-model:visible="modifyDeliverInfo.visible"
      :title="modifyDeliverInfo.title"
      :deliver-id="modifyDeliverInfo.deliverId"
      :receivingSourceOrderNo="modifyDeliverInfo.receivingSourceOrderNo"
      :order-id="modifyDeliverInfo.orderId"
      @returnModify="getList"
    ></ModifyOrder>
  </div>
</template>
<script setup name="pendDealAssetDeliverOrder">
import { getPendReceivingDeliverOrderList } from '@/api/supplier.js'
import { getSupplierAll } from '@/api/core'
import PageDialog from '@/components/PageDialog/index'
import ModifyOrder from '@/views/asset/assetManage/assetReceiving/modifyByDeliver'
import InvoiceDetail from '@/views/deliver/Invoice/deviceDetail'
import { mapGetters } from 'vuex'
const { proxy } = getCurrentInstance()
const page = ref({
  curPage: 1, // 当前页
  pageSize: 20, // 每页条数
})
// 过滤相关配置
const filterInfo = ref({
  query: {
    deliverProductType: '2',
    supplierAuth: 'supplierId',
  },
  searchList: [
    {
      componentType: 'input',
      label: '发货单号',
      value: 'orderNo',
      event: search,
    },
    {
      componentType: 'input',
      label: '采购单号',
      value: 'purchNo',
      event: search,
    },
    {
      componentType: 'slip-select',
      value: 'supplierIdMult',
      label: '供应商',
      api: getSupplierAll,
      query: { supplierAuth: 'supplierId', sort: 'create_time desc' },
      clearable: true,
      filterable: true,
      keyField: 'supplierName',
      valueField: 'supplierId',
      event: search,
    },
  ],
  searchBtn: [{ type: 'button', label: '搜索', event: search }],
  btnList: [],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  api: getPendReceivingDeliverOrderList,
  summaryFieldList: [
    { value: 'qty', formatter: proxy.$fn.qtyFormatter },
    { value: 'sumAmount', formatter: proxy.$fn.TripartiteMethod },
  ],
  data: [],
  selections: [],
  fieldList: [
    { label: '发货单号', value: 'orderNo', type: 'slot' },
    { label: '采购单号', value: 'purchNo' },
    { label: '供应商', value: 'supplierName' },
    { label: '状态', value: 'statusValue' },
    { label: '备注', value: 'remark' },
  ],
  handle: {
    fixed: 'right',
    label: '操作',
    width: '50',
    btList: [
      {
        label: '收货',
        event: receiving,
        show: proxy.$permission('pendDealAssetDeliverOrder:receiving'),
        ifRender: function (row) {
          return row.status === 2 || row.status === 3
        },
      },
    ],
  },
})
const detailInfo = ref({
  visible: false,
  id: '',
  receiveOrderSourceType: '',
})
const modifyDeliverInfo = ref({
  visible: false,
  title: '',
  orderId: '',
  deliverId: '',
  receiveAll: true,
})
const clearType = ref(true)

const toPage = computed(() => {
  return proxy.$store.state.settings.toPage
})
let stopWatch = function() {}
onActivated(() => {
  stopWatch = watch(
  () => toPage.value,
  (val) => {
    if (val) {
      let menus = proxy.$store.state.permission.defaultMenus
      menus.forEach((item) => {
        if (item.children) {
          item.children.forEach((el) => {
            if (el.code === 'pendDealAssetDeliverOrder' && el.url === val.url) {
              modifyDeliverInfo.value.visible = false
              detailInfo.value.visible = false
              if (val.urlType === 1) {
                getList()
              } else if (val.urlType === 2) {
                proxy.$nextTick(() => {
                  detailInfo.value.id = val.businessId
                  detailInfo.value.visible = true
                  sessionStorage.setItem('storeRowId', val.businessId)
                })
              }
              proxy.$store.dispatch('settings/toPage', '')
            }
          })
        }
      })
    }
  },
  {
    deep: true,
    immediate: true,
  }
)})

onDeactivated(() => {
  stopWatch()
})

onMounted(() => {
  getList()
})
function receiving(row) {
  //收货
  modifyDeliverInfo.value.deliverId = row.id
  modifyDeliverInfo.value.orderId = ''
  modifyDeliverInfo.value.visible = true
  modifyDeliverInfo.value.title = '新增收货单'
  modifyDeliverInfo.value.receiveAll = true
  modifyDeliverInfo.value.receivingSourceOrderNo = row.orderNo
}
function pageInfo(obj) {
  page.value = obj
}
// 获取列表
function getList() {
  tableInfo.value.refresh = Math.random()
}
// 搜索
function search() {
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
function showDetail(row) {
  detailInfo.value.id = row.id
  detailInfo.value.visible = true
  detailInfo.value.receiveOrderSourceType = row.receiveOrderSourceType
}
// 触发事件
function handleEvent(event, data) {
  switch (event) {
    case 'tableCheck':
      tableInfo.value.selections = [...data]
      break
  }
}
function sortChange(column) {
  if (filterInfo.value.query.customizeUserVO.head.sortType === 0) {
    let arr = proxy.$fn.sortChange(column, filterInfo.value.sortArr)
    filterInfo.value.sortArr = arr
    filterInfo.value.query.sortString = arr.toString()
    getList()
  }
}
</script>

<style scoped></style>
